/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				AttackBOPlan
 
Function						: 				Provides a Basic Operator Plan 
												for Attack through the ground
****************************************************************************/
package planninglanguage.plans;

import org.jdom.Element;

import planninglanguage.conditions.Condition;
import planninglanguage.conditions.StatusCondition;
import planninglanguage.conditions.UnitExistsCondition;
import util.XMLWriter;
import base.ProxyBot;

public class AttackGroundBOPlan extends BasicOperatorPlan {

	int m_x;
	int m_y;
	
	public AttackGroundBOPlan(int a_unitID,int a_x,int a_y,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_x=a_x;
		m_y=a_y;
		m_precondition = new UnitExistsCondition(m_unitID);
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = new StatusCondition(m_unitID,"attack");
	}
	
	public AttackGroundBOPlan(int a_unitID,int a_x,int a_y,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition, Plan a_parent)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_x=a_x;
		m_y=a_y;
		m_precondition = new UnitExistsCondition(m_unitID);
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = new StatusCondition(m_unitID,"attack");
		m_parent = a_parent;
	}
	
	public Object clone()
	{
		AttackGroundBOPlan l_p = new AttackGroundBOPlan(m_unitID,m_x,m_y,null,null,null, m_parent);
		l_p.setContext(m_context);
		l_p.m_originalUnit = m_originalUnit;
		return l_p;
	}
	
	public int getX() {
		return m_x;
	}
	
	public int getY() {
		return m_y;
	}

	public boolean send()
	{

	    ProxyBot.getProxyBot().attackGround(m_unitID, m_x,m_y,false);
		
		return true;
	}
	
	public String toString(int tabs) {
		String str= "";
		int i;
		
		for(i=0;i<tabs;i++) str = str + "  ";
		str = str + "AttackGroundBOPlan(status = " + STATUS_NAME[m_status] + " , unitID = " + m_unitID + " x = " + m_x + " y = " + m_y + " ) ";
		str = str + conditionsToString(tabs);
		return str;
	}	

	public boolean sameGoal(Object other)
	{
		if (getClass() != other.getClass()) return false;

		AttackGroundBOPlan o = (AttackGroundBOPlan) other;
		   
		if (m_unitID!=o.m_unitID) return false;
		if (m_x!=o.m_x) return false;   
		if (m_y!=o.m_y) return false;   
		   
		return true;
	}		
	
	public void writeXML(XMLWriter w) {
		w.tag(super.XMLopenTag() + " type=\"" + 
				this.getClass().getSimpleName() + "\"");		
		{
			w.tag(Plan.XML_X, m_x);
			w.tag(Plan.XML_Y, m_y);
			
			super.writeBOPlanAttrsXML(w);
		}
		w.tag(super.XMLcloseTag());
	}	
	public static AttackGroundBOPlan loadFromXML(Element e) {
		int x = Integer.parseInt(e.getChildText(Plan.XML_X));
		int y = Integer.parseInt(e.getChildText(Plan.XML_Y));
		
		AttackGroundBOPlan plan = new AttackGroundBOPlan(-1, x, y, null, null, null);
		return plan;
	}
}
